package com.gy.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.gy.R;

/**
 * TODO: document your custom view class.
 */
public class BooleanView extends View {

    private Paint mPaint;
    private Path mPath;
    private int mWidth;
    private int mHeight;
    private float currentValue;
    private Bitmap mArrowBitmap;
    private PathMeasure mPathMeasure;
    float[] pos = new float[2];
    float[] tan = new float[2];
    private long mPre;
    private long now;
    private Rect src;
    private RectF dst;
    private float len =30;

    public BooleanView(Context context) {
        super(context);
        init(null, 0);
    }

    public BooleanView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    public BooleanView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(4f);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPath = new Path();
        mPath.addCircle(0, 0, 150, Path.Direction.CW);
        mPathMeasure = new PathMeasure(mPath, false);
        mArrowBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.arrow);
        src =new Rect(0,0,mArrowBitmap.getWidth(),mArrowBitmap.getHeight());
        dst =new RectF();
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mWidth / 2, mHeight / 2);
        currentValue += 0.005;
        if (currentValue >= 1) {
            currentValue = 0;
            now = SystemClock.currentThreadTimeMillis();
            Log.d("BooleanView", "周期: "+ (now-mPre));
            mPre =now;
            
        }
        mPaint.setStrokeWidth(4f);
        canvas.drawPath(mPath, mPaint);
        mPaint.setStrokeWidth(34f);
        dst.left = 150-len;
        dst.top = -len;
        dst.right = 150+len;
        dst.bottom = +len;
        canvas.drawPoint(pos[0],pos[1],mPaint);
        canvas.rotate((float) Math.toDegrees(Math.atan2(tan[1],tan[0])-Math.PI/2));
        canvas.drawBitmap(mArrowBitmap,src,dst, mPaint);

        Log.d("BooleanView", "每次: "+ SystemClock.currentThreadTimeMillis());
        if (mPathMeasure.getPosTan(mPathMeasure.getLength() * currentValue, pos, tan)) {
            invalidate();
        }
    }
}
